/*
 * TraceStack.c
 *
 *  Created on: 2012/08/03
 *      Author: guanxiaoying
 */
#include "../tracedef.h"
#include "../../table/core_table.h"
#include "trace_cmd.h"

S4	Tr_StkCmdProcess(U2 usDo )
{
	S4 rc = TRACEEXIT_SUCCESS;
	AST astMaxCmdStack;
	if( usDo == STACK_PUSH )
	{
		stCmdStackAccess.alloc(1);
		astMaxCmdStack = stCmdStackAccess.getMax();

		stCmdStackAccess.currObject.set( astMaxCmdStack, stCmdCurrTraceInfo.astTable );
		stCmdStackAccess.traceFunction.set( astMaxCmdStack, stCmdCurrTraceInfo.astFunction );
		stCmdStackAccess.nextObject.set( astMaxCmdStack, stCmdCurrTraceInfo.astNext );
		stCmdStackAccess.pathFlag.set( astMaxCmdStack, stCmdCurrTraceInfo.pathFlag );

	}else{
		astMaxCmdStack = stCmdStackAccess.getMax();
		if (astMaxCmdStack  > 0 )
		{
			stCmdCurrTraceInfo.astNext = stCmdStackAccess.nextObject.get( astMaxCmdStack );
			stCmdCurrTraceInfo.astTable = stCmdStackAccess.currObject.get( astMaxCmdStack );
			stCmdCurrTraceInfo.astFunction = stCmdStackAccess.traceFunction.get( astMaxCmdStack );
			stCmdCurrTraceInfo.pathFlag = stCmdStackAccess.pathFlag.get( astMaxCmdStack );
			stCmdStackAccess.alloc(-1);
		}
		else
		{
			stCmdCurrTraceInfo.astTable = 0;
			stCmdCurrTraceInfo.astFunction = 0;
			stCmdCurrTraceInfo.astNext = 0;
			stCmdCurrTraceInfo.pathFlag = PATH_TRUE;
			rc = TRACEEXIT_END;
		}
	}
	return rc;
}
S4	Tr_StkPathProcess(U2 usDo ){
	S4 rc = TRACEEXIT_SUCCESS;
	AST astMaxPathStack;
	if( usDo == STACK_PUSH )
	{
		if( stCmdTraceOption.pCallBackFunction != NULL ){
			stCmdTraceOption.pCallBackFunction( stCmdCurrTraceInfo.astPath, EVENT_PATH_PUSH );
		}
		stTrPathStkAccess.alloc(1);
		astMaxPathStack = stTrPathStkAccess.getMax();

		stTrPathStkAccess.currPathInfo.set( astMaxPathStack, stCmdCurrTraceInfo.astPath );
		stTrPathStkAccess.nextPathInfo.set( astMaxPathStack, stCmdCurrTraceInfo.astPathNext );
		stTrPathStkAccess.pathParam.set( astMaxPathStack, stCmdCurrTraceInfo.astPathParam );
	}else{
		astMaxPathStack = stTrPathStkAccess.getMax();
		if ( astMaxPathStack > stCmdCurrTraceInfo.astPathStkBottom )
		{
			stCmdCurrTraceInfo.astPath = stTrPathStkAccess.currPathInfo.get( astMaxPathStack );
			stCmdCurrTraceInfo.astPathParam = stTrPathStkAccess.pathParam.get( astMaxPathStack );
			stCmdCurrTraceInfo.astPathNext = stTrPathStkAccess.nextPathInfo.get( astMaxPathStack );
			stTrPathStkAccess.alloc(-1);
		}
		else
		{
			stCmdCurrTraceInfo.astPath = 0;
			stCmdCurrTraceInfo.astPathParam = 0;
			stCmdCurrTraceInfo.astPathNext = 0;
			rc = TRACEEXIT_END;
		}
		if( stCmdTraceOption.pCallBackFunction != NULL ){
			stCmdTraceOption.pCallBackFunction( stCmdCurrTraceInfo.astPath, EVENT_PATH_POP );
		}
	}
	return rc;
}
S4	Tr_StkLoopProcess(U2 usDo ){
	S4 rc = TRACEEXIT_SUCCESS;
	AST astMaxLoopStack;
	AST astMaxFuncStack;
	if( usDo == STACK_PUSH )
	{
		if( stCmdTraceOption.pCallBackFunction != NULL ){
			stCmdTraceOption.pCallBackFunction( stCmdCurrTraceInfo.astLoop, EVENT_LOOP_PUSH );
		}
		stTrLoopStkAccess.alloc(1);
		astMaxLoopStack = stTrLoopStkAccess.getMax();

		stTrLoopStkAccess.loop.set( astMaxLoopStack, stCmdCurrTraceInfo.astLoop );
		stTrLoopStkAccess.loopExitStkBottom.set( astMaxLoopStack, stCmdCurrTraceInfo.astLoopExitBottom );
		stTrLoopStkAccess.pathStkBottom.set( astMaxLoopStack, stCmdCurrTraceInfo.astPathStkBottom );
		stTrLoopStkAccess.loopFlg.set( astMaxLoopStack, stCmdCurrTraceInfo.loopFlg );
	}else{
		astMaxLoopStack = stTrLoopStkAccess.getMax();
		astMaxFuncStack = stFuncStkAccess.getMax();
		if ( astMaxLoopStack > stFuncStkAccess.loopStackMax.get( astMaxFuncStack ) )
		{
			stCmdCurrTraceInfo.astLoop = stTrLoopStkAccess.loop.get( astMaxLoopStack );
			stCmdCurrTraceInfo.astLoopExitBottom = stTrLoopStkAccess.loopExitStkBottom.get( astMaxLoopStack );
			stCmdCurrTraceInfo.astPathStkBottom = stTrLoopStkAccess.pathStkBottom.get( astMaxLoopStack );
			stCmdCurrTraceInfo.loopFlg  = stTrLoopStkAccess.loopFlg.get( astMaxLoopStack );
			stTrLoopStkAccess.alloc(-1);
		}
		else
		{
			stCmdCurrTraceInfo.astLoop = 0;
			stCmdCurrTraceInfo.astLoopExitBottom = 0;
			stCmdCurrTraceInfo.astPathStkBottom = 0;
			stCmdCurrTraceInfo.loopFlg = LOOP_FLG_NONE;
		}
		if( stCmdTraceOption.pCallBackFunction != NULL ){
			stCmdTraceOption.pCallBackFunction( stCmdCurrTraceInfo.astLoop, EVENT_LOOP_POP );
		}
	}
	return rc;
}
S4	Tr_StkFuncProcess(U2 usDo ){
	S4 rc = TRACEEXIT_SUCCESS;
	AST astMaxFuncStack;
	AST astPathStack;
	AST astLoopStack;
	if( usDo == STACK_PUSH )
	{
		stFuncStkAccess.alloc(1);
		astMaxFuncStack = stFuncStkAccess.getMax();
		stFuncStkAccess.cmdList.set( astMaxFuncStack, stCmdCurrTraceInfo.astTable );
		stFuncStkAccess.nextCmdList.set( astMaxFuncStack, stCmdCurrTraceInfo.astNext );
		stFuncStkAccess.loopFlg.set( astMaxFuncStack, stCmdCurrTraceInfo.loopFlg );
		stFuncStkAccess.loop.set( astMaxFuncStack, stCmdCurrTraceInfo.astLoop );
		stFuncStkAccess.path.set( astMaxFuncStack, stCmdCurrTraceInfo.astPath );
		stFuncStkAccess.nextPath.set( astMaxFuncStack, stCmdCurrTraceInfo.astPathNext );
		stFuncStkAccess.pathParam.set( astMaxFuncStack, stCmdCurrTraceInfo.astPathParam );
		stFuncStkAccess.function.set( astMaxFuncStack, stCmdCurrTraceInfo.astFunction );
		stFuncStkAccess.pathStackBottom.set( astMaxFuncStack, stCmdCurrTraceInfo.astPathStkBottom);
		stFuncStkAccess.loopExitBottom.set( astMaxFuncStack, stCmdCurrTraceInfo.astLoopExitBottom );
		stFuncStkAccess.pathParamMax.set( astMaxFuncStack, Tr_PathParamGetMax() );
		stFuncStkAccess.loopStackMax.set( astMaxFuncStack, stTrLoopStkAccess.getMax() );
		stFuncStkAccess.pathStackMax.set( astMaxFuncStack, stTrPathStkAccess.getMax() );
	}else{
		astMaxFuncStack = stFuncStkAccess.getMax();
		if (astMaxFuncStack >= MIN_ARRAY )
		{
			astPathStack = stFuncStkAccess.pathStackMax.get( astMaxFuncStack );
			astLoopStack = stFuncStkAccess.loopStackMax.get( astMaxFuncStack );
			assert( astPathStack == stTrPathStkAccess.getMax());
			assert( astLoopStack == stTrLoopStkAccess.getMax());
			while( astPathStack < stTrPathStkAccess.getMax() ){
				Tr_StkPathProcess( STACK_PULL );
			}
			while( astLoopStack < stTrLoopStkAccess.getMax() ){
				Tr_StkLoopProcess( STACK_PULL );
			}
			Tr_PathParamClearAfter( stFuncStkAccess.pathParamMax.get( astMaxFuncStack ));

			stCmdStackAccess.clear();
			stCmdCurrTraceInfo.astFunction = stFuncStkAccess.function.get( astMaxFuncStack );
			stCmdCurrTraceInfo.astPathStkBottom = stFuncStkAccess.pathStackBottom.get( astMaxFuncStack );
			stCmdCurrTraceInfo.astLoopExitBottom = stFuncStkAccess.loopExitBottom.get( astMaxFuncStack );
			stCmdCurrTraceInfo.astTable  = stFuncStkAccess.cmdList.get( astMaxFuncStack );
			stCmdCurrTraceInfo.astNext = stFuncStkAccess.nextCmdList.get( astMaxFuncStack );
			stCmdCurrTraceInfo.loopFlg = stFuncStkAccess.loopFlg.get( astMaxFuncStack );
			stCmdCurrTraceInfo.astLoop = stFuncStkAccess.loop.get( astMaxFuncStack );
			stCmdCurrTraceInfo.astPath = stFuncStkAccess.path.get( astMaxFuncStack );
			stCmdCurrTraceInfo.astPathNext = stFuncStkAccess.nextPath.get( astMaxFuncStack );
			stCmdCurrTraceInfo.astPathParam = stFuncStkAccess.pathParam.get( astMaxFuncStack );
			stFuncStkAccess.alloc(-1);
		}else{
			assert(stCmdCurrTraceInfo.astPathStkBottom == 0);
			assert(stCmdCurrTraceInfo.astLoopExitBottom == 0);
			stCmdCurrTraceInfo.astFunction = 0;
			stCmdCurrTraceInfo.astPathStkBottom = 0;
			stCmdCurrTraceInfo.astLoopExitBottom = 0;
			stCmdCurrTraceInfo.astTable  = 0;
			stCmdCurrTraceInfo.astNext = 0;
			stCmdCurrTraceInfo.loopFlg = LOOP_FLG_NONE;
			stCmdCurrTraceInfo.astLoop = 0;
			stCmdCurrTraceInfo.astPath = 0;
			stCmdCurrTraceInfo.astPathNext =0;
			stCmdCurrTraceInfo.astPathParam = 0;
			Tr_VarBlkClear();
			Tr_VariClear();

		}
	}
	return rc;
}
